<?php
/**
 * Descript:全局方法
 * User: lufeng501206@gmail.com
 * Date: 2017/3/7 23:27
 */

/**
 * 返回json格式相应
 * @param array $data
 * @param int $state
 * @return \Illuminate\Http\JsonResponse\
 */
function successJson($data = array(), $msg = 'ok', $state = 1)
{
    $response = [];
    $response['data'] = $data;
    $response['msg'] = $msg;
    $response['state'] = $state;
    return response()->json($response);
}

/**
 * 返回json格式相应
 * @param array $data
 * @param int $state
 * @return \Illuminate\Http\JsonResponse\
 */
function errorJson($data = array(), $msg = 'fail', $state = 0)
{
    $response = [];
    $response['data'] = $data;
    $response['msg'] = $msg;
    $response['state'] = $state;
    return response()->json($response);
}

function rebulidDataIndex($data, $index = 'id')
{
    $formatData = [];
    if (!empty($data)) {
        foreach ($data as $key => $value) {
            $formatData[$value[$index]] = $value;
        }
    }
    return $formatData;
}